import { EggContext, Next } from '@eggjs/tegg';
import { ErrorBody } from 'typings/app/index';

export async function responseFormatter(ctx: EggContext, next: Next) {
  try {
    await next();
    ctx.body = {
      code: 0,
      msg: 'ok',
      data: ctx.body,
    };
  } catch (e) {
    const errorInfo = e as ErrorBody;
    console.log(e);
    ctx.body = {
      code: errorInfo.code || 500,
      msg: errorInfo.message || 'unknown error',
      data: {},
    };
  }

}
